<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >

      <el-form-item label="渠道名称" prop="channelName">
        <el-select
          v-model="queryParams.channelName"
          placeholder="请选择渠道"
          clearable
          class="!w-240px"
        >
          <el-option label="车妈妈" value="车妈妈"/>
          <el-option label="玖达" value="玖达"/>
          <el-option label="人保财险" value="人保财险"/>
        </el-select>
      </el-form-item>


      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>

      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" border :data="list" :stripe="true" :show-overflow-tooltip="true">

      <el-table-column fixed="left" label="操作" width="60" align="center">

        <template #default="scope">
          <el-popover style="position: relative" :visible="popoverVisible == scope.row.id"
                      placement="bottom-start"
                      :width="60">
<!--            <el-row class="mb-8px">-->
<!--              <el-col :span="24">-->
<!--                <el-button-->
<!--                  link-->
<!--                  type="primary"-->
<!--                  @click="handlePic(scope.row)"-->
<!--                >-->
<!--                  上传图片-->
<!--                </el-button>-->
<!--              </el-col>-->
<!--            </el-row>-->


<!--            <el-row class="mb-8px">-->
<!--              <el-col :span="24">-->
<!--                <el-button-->
<!--                  link-->
<!--                  type="primary"-->
<!--                  @click="handleChangeRemark(scope.row)"-->
<!--                >-->
<!--                  修改备注-->
<!--                </el-button>-->
<!--              </el-col>-->
<!--            </el-row>-->

            <el-row class="mb-8px">
              <el-col :span="24">
                <el-button
                  link
                  type="primary"
                  @click="handleAddReceipt(scope.row)"
                >
                  添加收款单
                </el-button>
              </el-col>
            </el-row>

            <div class="popoverCloseBtn" @click="closePopover()">关闭</div>
            <template #reference>
              <img src="@/assets/imgs/more_edit.png" class="linkBtn" @click="lookBind(scope.row)" @contextmenu.prevent="handleRightClick(scope.row.id)"
              />
            </template>
          </el-popover>
        </template>
      </el-table-column>


      <el-table-column label="渠道名称"  prop="channelName" />
      <el-table-column label="合计金额" width="100px"  prop="amount" />
      <el-table-column label="已收金额" width="100px" prop="receiptAmount" >
        <template #default="scope">
          <el-input class="no-border-input"
                    @change="handleChangeReceiptAmount(scope.row)" type="text" v-model="scope.row.receiptAmount"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />

    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ReceiptBatchForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { ReceiptBatchApi, ReceiptBatchVO } from '@/api/dispatch/receiptbatch'
import ReceiptBatchForm from './ReceiptBatchForm.vue'
import {ElMessage} from "element-plus";
import {PaymentBatchApi} from "@/api/dispatch/paymentbatch";

/** 派单批量收款单 列表 */
defineOptions({ name: 'DispatchReceiptBatch' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<ReceiptBatchVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  dispatchReceiptIds: undefined,
  amount: undefined,
  receiptAmount: undefined,
  channelName: undefined,
  remark: undefined,
  status: undefined,
  sort: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ReceiptBatchApi.getReceiptBatchPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}


const {push} = useRouter()

const  lookBind = (row) => {
  push({
    path: "/dispatch/finance/receipt/receipt",
    query: {doType: "query", data: row.dispatchReceiptIds}
  });
}

const handleAddReceipt = (row) => {
  message.warning('开发中')
}

const popoverVisible = ref(null);
const closePopover = async () => {
  popoverVisible.value = null;
}
const handleRightClick = (id) => {
  popoverVisible.value = id;
};

/**
 * 已收金额
 * @param row
 */
const handleChangeReceiptAmount = async (row) => {
  try{

    //金额不能小于0
    if (row.receiptAmount < 0 || row.receiptAmount == null || row.receiptAmount == '') {
      ElMessage.error('已收金额不能小于0');
      return;
    }

    await message.confirm('确定要修改已收金额吗?');

    await ReceiptBatchApi.updateReceiptAmount(row);

    //提示
    ElMessage.success('修改成功');

  }finally {
    getList();
  }

}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ReceiptBatchApi.deleteReceiptBatch(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ReceiptBatchApi.exportReceiptBatch(queryParams)
    download.excel(data, '派单批量收款单.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
onActivated(() => {
  getList()
})
</script>
